package com.hunttown.mes.service;

import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.ScriptFilterUtils;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.AnalysisManageGroupDao;
import com.hunttown.mes.domain.AnalysisManageGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2021-08-31 14:23:16
 */
@Service
public class AnalysisManageGroupService {

    private final static Logger logger = LoggerFactory.getLogger(AnalysisManageGroupService.class);

    private final AnalysisManageGroupDao analysisManageGroupDao;
    private final AHelpService helpService;

    @Autowired
    public AnalysisManageGroupService(AnalysisManageGroupDao analysisManageGroupDao, AHelpService helpService) {
        this.analysisManageGroupDao = analysisManageGroupDao;
        this.helpService = helpService;
    }

    /**
     * 新增
     *
     * @param analysisManageGroup 实体对象
     * @return 最新插入id
     */
    public AnalysisManageGroup insert(AnalysisManageGroup analysisManageGroup) {
        //api接口传进来是有值的
        if (analysisManageGroup.getEntId() == null)
            analysisManageGroup.setEntId(helpService.getEnterpriseId());
        if (analysisManageGroup.getAdminId() == null)
            analysisManageGroup.setAdminId(0);
        if (analysisManageGroup.getCreateTime() == null)
            analysisManageGroup.setCreateTime(new Date());
        if (analysisManageGroup.getStopFlag() == null)
            analysisManageGroup.setStopFlag(0);
        if (analysisManageGroup.getDeleteFlag() == null)
            analysisManageGroup.setDeleteFlag(0);

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(analysisManageGroup), AnalysisManageGroup.class))) {
            return analysisManageGroup;
        }
        return analysisManageGroupDao.insert(analysisManageGroup);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param analysisManageGroup 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(AnalysisManageGroup analysisManageGroup) {
        if (analysisManageGroup.getEntId() == null) {
            analysisManageGroup.setEntId(helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(analysisManageGroup), AnalysisManageGroup.class))) {
            return false;
        }
        return analysisManageGroupDao.updateInfoById(analysisManageGroup);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForQuery(query)) {
            return false;
        }
        return analysisManageGroupDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public AnalysisManageGroup selectById(Integer id) {
        return analysisManageGroupDao.selectById(id, helpService.getEnterpriseId());
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public AnalysisManageGroup selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<AnalysisManageGroup> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<AnalysisManageGroup> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return analysisManageGroupDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return analysisManageGroupDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return analysisManageGroupDao.deleteById(id, helpService.getEnterpriseId());
    }

}